package com.chen.recruit.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.chen.recruit.common.annotation.Log;
import com.chen.recruit.common.api.CommonResult;
import com.chen.recruit.common.enums.BusinessType;
import com.chen.recruit.common.utils.ExcelUtils;
import com.chen.recruit.common.valid.AddGroup;
import com.chen.recruit.common.valid.UpdateGroup;
import com.chen.recruit.domain.Role;
import com.chen.recruit.domain.model.PageQuery;
import com.chen.recruit.service.RoleService;
import lombok.RequiredArgsConstructor;

import javax.servlet.http.HttpServletResponse;

import cn.dev33.satoken.annotation.SaCheckPermission;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.chen.recruit.domain.vo.RoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "角色管理 RoleController")
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/role")
public class RoleController {

    private final RoleService roleService;

    @ApiOperation("新增角色")
    @SaCheckPermission("system:role:add")
    @Log(module = "系统管理-角色管理", businessType = BusinessType.INSERT)
    @PostMapping
    public CommonResult add(@Validated(AddGroup.class) @RequestBody RoleVo vo) {
        return roleService.addOrUpdateRole(vo);
    }

    @ApiOperation("删除角色")
    @SaCheckPermission("system:role:delete")
    @Log(module = "系统管理-角色管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public CommonResult delete(@PathVariable Integer[] ids) {
        return roleService.deleteRoleByIds(ids);
    }

    @ApiOperation("更新角色")
    @SaCheckPermission("system:role:update")
    @Log(module = "系统管理-角色管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public CommonResult update(@Validated(UpdateGroup.class) @RequestBody RoleVo vo) {
        return roleService.addOrUpdateRole(vo);
    }

    @ApiOperation("获取角色详细信息")
    @SaCheckPermission("system:role:update")
    @GetMapping("/{id}")
    public CommonResult getInfo(@PathVariable Integer id) {
        return CommonResult.success(roleService.getById(id));
    }

    @ApiOperation("查询角色列表")
    @SaCheckPermission("system:role:list")
    @GetMapping("/list")
    public CommonResult list(RoleVo roleVo, PageQuery pageQuery) {
        return CommonResult.success(roleService.page(pageQuery.build(), buildQueryWrapper(roleVo)));
    }

    @ApiOperation("导出角色列表")
    @SaCheckPermission("system:role:export")
    @Log(module = "系统管理-角色管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response) {
        ExcelUtils.exportExcel(response, "角色表", Role.class, roleService.list());
    }

    @GetMapping("/menu/{roleId}")
    @ApiOperation("获取指定角色的权限列表id")
    public CommonResult getMenuIdsByRoleId(@PathVariable Integer roleId) {
        return CommonResult.success(roleService.getMenuIdsByRoleId(roleId));
    }

    private LambdaQueryWrapper<Role> buildQueryWrapper(RoleVo vo) {
        return Wrappers.<Role>lambdaQuery()
                .eq(ObjectUtil.isNotNull(vo.getStatus()), Role::getStatus, vo.getStatus())
                .like(StringUtils.isNotBlank(vo.getName()), Role::getName, vo.getName());
    }

}
